package com.isdepci.project.factor.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.isdepci.project.factor.domain.CarbonFactorInfo;
import com.isdepci.project.factor.domain.param.FactorItem;
import com.isdepci.project.factor.utils.FactorRedisUtils;
import com.isdepci.project.factor.utils.FactorUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.isdepci.framework.aspectj.lang.annotation.Log;
import com.isdepci.framework.aspectj.lang.enums.BusinessType;
import com.isdepci.project.factor.domain.CategorizeInfo;
import com.isdepci.project.factor.service.ICategorizeInfoService;
import com.isdepci.framework.web.controller.BaseController;
import com.isdepci.framework.web.domain.AjaxResult;

/**
 * 分类管理Controller
 *
 * @author isdepci
 * @date 2024-10-09
 */
@RestController
@RequestMapping("/factor/categorizeInfo")
public class CategorizeInfoController extends BaseController {
    @Autowired
    private ICategorizeInfoService categorizeInfoService;
    @Autowired
    private FactorUtils factorUtils;

    @GetMapping("/dict")
    public AjaxResult selectDictList() {
        List<CategorizeInfo> list = factorUtils.selectCategorizeDictList();
        List<CarbonFactorInfo> factorInfos = factorUtils.selectCarbonFactorInfoList();
        Map<String, List<CarbonFactorInfo>> groupMap = factorInfos.stream().collect(Collectors.groupingBy(CarbonFactorInfo::getCategorizeCode));
        for (CategorizeInfo categorizeInfo : list) {
            if (categorizeInfo.getLevel()!=3){
                continue;
            }
            String code = categorizeInfo.getAncestors().substring(2)+","+categorizeInfo.getId();
            List<CarbonFactorInfo> factorInfoList =  groupMap.get(code);
            if(factorInfoList==null||factorInfoList.size()<=0){
                continue;
            }
            List<FactorItem> items = new ArrayList<>();
            for (CarbonFactorInfo carbonFactorInfo : factorInfoList) {
                if(carbonFactorInfo.getUpstreamEmissions()!=null){
                    items.add(new FactorItem(carbonFactorInfo.getId(),carbonFactorInfo.getUpstreamEmissions(),carbonFactorInfo.getUpstreamUnit(),1));
                }
                if(carbonFactorInfo.getDownstreamEmissions()!=null){
                    items.add(new FactorItem(carbonFactorInfo.getId(),carbonFactorInfo.getDownstreamEmissions(),carbonFactorInfo.getDownstreamUnit(),2));
                }
            }
            categorizeInfo.setFactorItemList(items);
        }
        return AjaxResult.success(list);
    }



    /**
     * 查询分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('factor:categorizeInfo:list')")
    @GetMapping("/list")
    public AjaxResult list(CategorizeInfo categorizeInfo) {
        List<CategorizeInfo> list = categorizeInfoService.selectCategorizeInfoList(categorizeInfo);
        return AjaxResult.success(list);
    }

    /**
     * 获取分类管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('factor:categorizeInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(categorizeInfoService.selectCategorizeInfoById(id));
    }

    /**
     * 新增分类管理
     */
    @PreAuthorize("@ss.hasPermi('factor:categorizeInfo:add')")
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CategorizeInfo categorizeInfo) {
        return (categorizeInfoService.insertCategorizeInfo(categorizeInfo));
    }

    /**
     * 修改分类管理
     */
    @PreAuthorize("@ss.hasPermi('factor:categorizeInfo:edit')")
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CategorizeInfo categorizeInfo) {
        if (categorizeInfo.getParentId().equals(categorizeInfo.getId())){
            return AjaxResult.error("修改'" + categorizeInfo.getChineseName() + "'失败，上级分类不能是自己");
        }
        return (categorizeInfoService.updateCategorizeInfo(categorizeInfo));
    }

    /**
     * 删除分类管理
     */
    @PreAuthorize("@ss.hasPermi('factor:categorizeInfo:remove')")
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return (categorizeInfoService.deleteCategorizeInfoById(id));
    }


    @Log(title = "分类管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clearCache")
    public AjaxResult clearCache() {
        FactorRedisUtils.clearListCache();
        return AjaxResult.success();
    }

}
